﻿using System;
using System.Net;
using System.Net.Sockets;
/// <summary>
/// 用于连接其他服务器
/// </summary>
public class ServerConnectController : ITCPSocketServer
{
    /// <summary>
    /// 用于监听逻辑服务器的连接
    /// </summary>
    TCPSocketServer m_SocketServer;

    Action<EndPoint, Socket> m_OnNewConnnect;

    public void Start(int port,int maxConnectCount,Action<EndPoint, Socket> onNewConnect)
    {
        m_OnNewConnnect = onNewConnect;

        m_SocketServer = new TCPSocketServer(this,null,null);
        m_SocketServer.Start(port, maxConnectCount);
    }

    public void OnNewConnected(EndPoint endPoint, Socket connect)
    {
        GameDebug.Log($"ServerConnectController   OnNewConnected:{endPoint}");

        m_OnNewConnnect?.Invoke(endPoint, connect);
    }

    public void Connect(string address,int port,Action<Socket> onSuc,Action<string> onFailed)
    {
        TCPSocketServer serverSocket = new TCPSocketServer(null,null);

        serverSocket.BeginConnect(address, port, (connect, error) =>
               {
                   if (connect != null && connect.Connected)
                   {
                       onSuc?.Invoke(connect);
                   }
                   else
                   {
                       onFailed?.Invoke(error);
                   }
               });
    }

    public void OnReceiveData(EndPoint endPoint, byte[] data)
    {
       
    }

    public void OnRecOrSendFailed(EndPoint endPoint)
    {

    }
}

